/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entityprojet;

import entityprojet.exceptions.IllegalOrphanException;
import entityprojet.exceptions.NonexistentEntityException;
import entityprojet.exceptions.PreexistingEntityException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author T.Ron
 */
public class PeriodeJpaController implements Serializable {

    public PeriodeJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = Persistence.createEntityManagerFactory("projetT_RPU");

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Periode periode) throws PreexistingEntityException, Exception {
        if (periode.getPeriodePK() == null) {
            periode.setPeriodePK(new PeriodePK());
        }
        if (periode.getEcriturecCollection() == null) {
            periode.setEcriturecCollection(new ArrayList<Ecriturec>());
        }
        if (periode.getBudgetcgpbCollection() == null) {
            periode.setBudgetcgpbCollection(new ArrayList<Budgetcgpb>());
        }
        if (periode.getBudgetpbCollection() == null) {
            periode.setBudgetpbCollection(new ArrayList<Budgetpb>());
        }
        if (periode.getJoperiodeCollection() == null) {
            periode.setJoperiodeCollection(new ArrayList<Joperiode>());
        }
        periode.getPeriodePK().setNumexo(periode.getExercicecompta().getNumexo());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Exercicecompta exercicecompta = periode.getExercicecompta();
            if (exercicecompta != null) {
                exercicecompta = em.getReference(exercicecompta.getClass(), exercicecompta.getNumexo());
                periode.setExercicecompta(exercicecompta);
            }
            Collection<Ecriturec> attachedEcriturecCollection = new ArrayList<Ecriturec>();
            for (Ecriturec ecriturecCollectionEcriturecToAttach : periode.getEcriturecCollection()) {
                ecriturecCollectionEcriturecToAttach = em.getReference(ecriturecCollectionEcriturecToAttach.getClass(), ecriturecCollectionEcriturecToAttach.getIdEc());
                attachedEcriturecCollection.add(ecriturecCollectionEcriturecToAttach);
            }
            periode.setEcriturecCollection(attachedEcriturecCollection);
            Collection<Budgetcgpb> attachedBudgetcgpbCollection = new ArrayList<Budgetcgpb>();
            for (Budgetcgpb budgetcgpbCollectionBudgetcgpbToAttach : periode.getBudgetcgpbCollection()) {
                budgetcgpbCollectionBudgetcgpbToAttach = em.getReference(budgetcgpbCollectionBudgetcgpbToAttach.getClass(), budgetcgpbCollectionBudgetcgpbToAttach.getBudgetcgpbPK());
                attachedBudgetcgpbCollection.add(budgetcgpbCollectionBudgetcgpbToAttach);
            }
            periode.setBudgetcgpbCollection(attachedBudgetcgpbCollection);
            Collection<Budgetpb> attachedBudgetpbCollection = new ArrayList<Budgetpb>();
            for (Budgetpb budgetpbCollectionBudgetpbToAttach : periode.getBudgetpbCollection()) {
                budgetpbCollectionBudgetpbToAttach = em.getReference(budgetpbCollectionBudgetpbToAttach.getClass(), budgetpbCollectionBudgetpbToAttach.getBudgetpbPK());
                attachedBudgetpbCollection.add(budgetpbCollectionBudgetpbToAttach);
            }
            periode.setBudgetpbCollection(attachedBudgetpbCollection);
            Collection<Joperiode> attachedJoperiodeCollection = new ArrayList<Joperiode>();
            for (Joperiode joperiodeCollectionJoperiodeToAttach : periode.getJoperiodeCollection()) {
                joperiodeCollectionJoperiodeToAttach = em.getReference(joperiodeCollectionJoperiodeToAttach.getClass(), joperiodeCollectionJoperiodeToAttach.getIdJop());
                attachedJoperiodeCollection.add(joperiodeCollectionJoperiodeToAttach);
            }
            periode.setJoperiodeCollection(attachedJoperiodeCollection);
            em.persist(periode);
            if (exercicecompta != null) {
                exercicecompta.getPeriodeCollection().add(periode);
                exercicecompta = em.merge(exercicecompta);
            }
            for (Ecriturec ecriturecCollectionEcriturec : periode.getEcriturecCollection()) {
                Periode oldPeriodeOfEcriturecCollectionEcriturec = ecriturecCollectionEcriturec.getPeriode();
                ecriturecCollectionEcriturec.setPeriode(periode);
                ecriturecCollectionEcriturec = em.merge(ecriturecCollectionEcriturec);
                if (oldPeriodeOfEcriturecCollectionEcriturec != null) {
                    oldPeriodeOfEcriturecCollectionEcriturec.getEcriturecCollection().remove(ecriturecCollectionEcriturec);
                    oldPeriodeOfEcriturecCollectionEcriturec = em.merge(oldPeriodeOfEcriturecCollectionEcriturec);
                }
            }
            for (Budgetcgpb budgetcgpbCollectionBudgetcgpb : periode.getBudgetcgpbCollection()) {
                Periode oldPeriodeOfBudgetcgpbCollectionBudgetcgpb = budgetcgpbCollectionBudgetcgpb.getPeriode();
                budgetcgpbCollectionBudgetcgpb.setPeriode(periode);
                budgetcgpbCollectionBudgetcgpb = em.merge(budgetcgpbCollectionBudgetcgpb);
                if (oldPeriodeOfBudgetcgpbCollectionBudgetcgpb != null) {
                    oldPeriodeOfBudgetcgpbCollectionBudgetcgpb.getBudgetcgpbCollection().remove(budgetcgpbCollectionBudgetcgpb);
                    oldPeriodeOfBudgetcgpbCollectionBudgetcgpb = em.merge(oldPeriodeOfBudgetcgpbCollectionBudgetcgpb);
                }
            }
            for (Budgetpb budgetpbCollectionBudgetpb : periode.getBudgetpbCollection()) {
                Periode oldPeriodeOfBudgetpbCollectionBudgetpb = budgetpbCollectionBudgetpb.getPeriode();
                budgetpbCollectionBudgetpb.setPeriode(periode);
                budgetpbCollectionBudgetpb = em.merge(budgetpbCollectionBudgetpb);
                if (oldPeriodeOfBudgetpbCollectionBudgetpb != null) {
                    oldPeriodeOfBudgetpbCollectionBudgetpb.getBudgetpbCollection().remove(budgetpbCollectionBudgetpb);
                    oldPeriodeOfBudgetpbCollectionBudgetpb = em.merge(oldPeriodeOfBudgetpbCollectionBudgetpb);
                }
            }
            for (Joperiode joperiodeCollectionJoperiode : periode.getJoperiodeCollection()) {
                Periode oldPeriodeOfJoperiodeCollectionJoperiode = joperiodeCollectionJoperiode.getPeriode();
                joperiodeCollectionJoperiode.setPeriode(periode);
                joperiodeCollectionJoperiode = em.merge(joperiodeCollectionJoperiode);
                if (oldPeriodeOfJoperiodeCollectionJoperiode != null) {
                    oldPeriodeOfJoperiodeCollectionJoperiode.getJoperiodeCollection().remove(joperiodeCollectionJoperiode);
                    oldPeriodeOfJoperiodeCollectionJoperiode = em.merge(oldPeriodeOfJoperiodeCollectionJoperiode);
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findPeriode(periode.getPeriodePK()) != null) {
                throw new PreexistingEntityException("Periode " + periode + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Periode periode) throws IllegalOrphanException, NonexistentEntityException, Exception {
        periode.getPeriodePK().setNumexo(periode.getExercicecompta().getNumexo());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Periode persistentPeriode = em.find(Periode.class, periode.getPeriodePK());
            Exercicecompta exercicecomptaOld = persistentPeriode.getExercicecompta();
            Exercicecompta exercicecomptaNew = periode.getExercicecompta();
            Collection<Ecriturec> ecriturecCollectionOld = persistentPeriode.getEcriturecCollection();
            Collection<Ecriturec> ecriturecCollectionNew = periode.getEcriturecCollection();
            Collection<Budgetcgpb> budgetcgpbCollectionOld = persistentPeriode.getBudgetcgpbCollection();
            Collection<Budgetcgpb> budgetcgpbCollectionNew = periode.getBudgetcgpbCollection();
            Collection<Budgetpb> budgetpbCollectionOld = persistentPeriode.getBudgetpbCollection();
            Collection<Budgetpb> budgetpbCollectionNew = periode.getBudgetpbCollection();
            Collection<Joperiode> joperiodeCollectionOld = persistentPeriode.getJoperiodeCollection();
            Collection<Joperiode> joperiodeCollectionNew = periode.getJoperiodeCollection();
            List<String> illegalOrphanMessages = null;
            for (Ecriturec ecriturecCollectionOldEcriturec : ecriturecCollectionOld) {
                if (!ecriturecCollectionNew.contains(ecriturecCollectionOldEcriturec)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Ecriturec " + ecriturecCollectionOldEcriturec + " since its periode field is not nullable.");
                }
            }
            for (Budgetcgpb budgetcgpbCollectionOldBudgetcgpb : budgetcgpbCollectionOld) {
                if (!budgetcgpbCollectionNew.contains(budgetcgpbCollectionOldBudgetcgpb)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Budgetcgpb " + budgetcgpbCollectionOldBudgetcgpb + " since its periode field is not nullable.");
                }
            }
            for (Budgetpb budgetpbCollectionOldBudgetpb : budgetpbCollectionOld) {
                if (!budgetpbCollectionNew.contains(budgetpbCollectionOldBudgetpb)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Budgetpb " + budgetpbCollectionOldBudgetpb + " since its periode field is not nullable.");
                }
            }
            for (Joperiode joperiodeCollectionOldJoperiode : joperiodeCollectionOld) {
                if (!joperiodeCollectionNew.contains(joperiodeCollectionOldJoperiode)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Joperiode " + joperiodeCollectionOldJoperiode + " since its periode field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (exercicecomptaNew != null) {
                exercicecomptaNew = em.getReference(exercicecomptaNew.getClass(), exercicecomptaNew.getNumexo());
                periode.setExercicecompta(exercicecomptaNew);
            }
            Collection<Ecriturec> attachedEcriturecCollectionNew = new ArrayList<Ecriturec>();
            for (Ecriturec ecriturecCollectionNewEcriturecToAttach : ecriturecCollectionNew) {
                ecriturecCollectionNewEcriturecToAttach = em.getReference(ecriturecCollectionNewEcriturecToAttach.getClass(), ecriturecCollectionNewEcriturecToAttach.getIdEc());
                attachedEcriturecCollectionNew.add(ecriturecCollectionNewEcriturecToAttach);
            }
            ecriturecCollectionNew = attachedEcriturecCollectionNew;
            periode.setEcriturecCollection(ecriturecCollectionNew);
            Collection<Budgetcgpb> attachedBudgetcgpbCollectionNew = new ArrayList<Budgetcgpb>();
            for (Budgetcgpb budgetcgpbCollectionNewBudgetcgpbToAttach : budgetcgpbCollectionNew) {
                budgetcgpbCollectionNewBudgetcgpbToAttach = em.getReference(budgetcgpbCollectionNewBudgetcgpbToAttach.getClass(), budgetcgpbCollectionNewBudgetcgpbToAttach.getBudgetcgpbPK());
                attachedBudgetcgpbCollectionNew.add(budgetcgpbCollectionNewBudgetcgpbToAttach);
            }
            budgetcgpbCollectionNew = attachedBudgetcgpbCollectionNew;
            periode.setBudgetcgpbCollection(budgetcgpbCollectionNew);
            Collection<Budgetpb> attachedBudgetpbCollectionNew = new ArrayList<Budgetpb>();
            for (Budgetpb budgetpbCollectionNewBudgetpbToAttach : budgetpbCollectionNew) {
                budgetpbCollectionNewBudgetpbToAttach = em.getReference(budgetpbCollectionNewBudgetpbToAttach.getClass(), budgetpbCollectionNewBudgetpbToAttach.getBudgetpbPK());
                attachedBudgetpbCollectionNew.add(budgetpbCollectionNewBudgetpbToAttach);
            }
            budgetpbCollectionNew = attachedBudgetpbCollectionNew;
            periode.setBudgetpbCollection(budgetpbCollectionNew);
            Collection<Joperiode> attachedJoperiodeCollectionNew = new ArrayList<Joperiode>();
            for (Joperiode joperiodeCollectionNewJoperiodeToAttach : joperiodeCollectionNew) {
                joperiodeCollectionNewJoperiodeToAttach = em.getReference(joperiodeCollectionNewJoperiodeToAttach.getClass(), joperiodeCollectionNewJoperiodeToAttach.getIdJop());
                attachedJoperiodeCollectionNew.add(joperiodeCollectionNewJoperiodeToAttach);
            }
            joperiodeCollectionNew = attachedJoperiodeCollectionNew;
            periode.setJoperiodeCollection(joperiodeCollectionNew);
            periode = em.merge(periode);
            if (exercicecomptaOld != null && !exercicecomptaOld.equals(exercicecomptaNew)) {
                exercicecomptaOld.getPeriodeCollection().remove(periode);
                exercicecomptaOld = em.merge(exercicecomptaOld);
            }
            if (exercicecomptaNew != null && !exercicecomptaNew.equals(exercicecomptaOld)) {
                exercicecomptaNew.getPeriodeCollection().add(periode);
                exercicecomptaNew = em.merge(exercicecomptaNew);
            }
            for (Ecriturec ecriturecCollectionNewEcriturec : ecriturecCollectionNew) {
                if (!ecriturecCollectionOld.contains(ecriturecCollectionNewEcriturec)) {
                    Periode oldPeriodeOfEcriturecCollectionNewEcriturec = ecriturecCollectionNewEcriturec.getPeriode();
                    ecriturecCollectionNewEcriturec.setPeriode(periode);
                    ecriturecCollectionNewEcriturec = em.merge(ecriturecCollectionNewEcriturec);
                    if (oldPeriodeOfEcriturecCollectionNewEcriturec != null && !oldPeriodeOfEcriturecCollectionNewEcriturec.equals(periode)) {
                        oldPeriodeOfEcriturecCollectionNewEcriturec.getEcriturecCollection().remove(ecriturecCollectionNewEcriturec);
                        oldPeriodeOfEcriturecCollectionNewEcriturec = em.merge(oldPeriodeOfEcriturecCollectionNewEcriturec);
                    }
                }
            }
            for (Budgetcgpb budgetcgpbCollectionNewBudgetcgpb : budgetcgpbCollectionNew) {
                if (!budgetcgpbCollectionOld.contains(budgetcgpbCollectionNewBudgetcgpb)) {
                    Periode oldPeriodeOfBudgetcgpbCollectionNewBudgetcgpb = budgetcgpbCollectionNewBudgetcgpb.getPeriode();
                    budgetcgpbCollectionNewBudgetcgpb.setPeriode(periode);
                    budgetcgpbCollectionNewBudgetcgpb = em.merge(budgetcgpbCollectionNewBudgetcgpb);
                    if (oldPeriodeOfBudgetcgpbCollectionNewBudgetcgpb != null && !oldPeriodeOfBudgetcgpbCollectionNewBudgetcgpb.equals(periode)) {
                        oldPeriodeOfBudgetcgpbCollectionNewBudgetcgpb.getBudgetcgpbCollection().remove(budgetcgpbCollectionNewBudgetcgpb);
                        oldPeriodeOfBudgetcgpbCollectionNewBudgetcgpb = em.merge(oldPeriodeOfBudgetcgpbCollectionNewBudgetcgpb);
                    }
                }
            }
            for (Budgetpb budgetpbCollectionNewBudgetpb : budgetpbCollectionNew) {
                if (!budgetpbCollectionOld.contains(budgetpbCollectionNewBudgetpb)) {
                    Periode oldPeriodeOfBudgetpbCollectionNewBudgetpb = budgetpbCollectionNewBudgetpb.getPeriode();
                    budgetpbCollectionNewBudgetpb.setPeriode(periode);
                    budgetpbCollectionNewBudgetpb = em.merge(budgetpbCollectionNewBudgetpb);
                    if (oldPeriodeOfBudgetpbCollectionNewBudgetpb != null && !oldPeriodeOfBudgetpbCollectionNewBudgetpb.equals(periode)) {
                        oldPeriodeOfBudgetpbCollectionNewBudgetpb.getBudgetpbCollection().remove(budgetpbCollectionNewBudgetpb);
                        oldPeriodeOfBudgetpbCollectionNewBudgetpb = em.merge(oldPeriodeOfBudgetpbCollectionNewBudgetpb);
                    }
                }
            }
            for (Joperiode joperiodeCollectionNewJoperiode : joperiodeCollectionNew) {
                if (!joperiodeCollectionOld.contains(joperiodeCollectionNewJoperiode)) {
                    Periode oldPeriodeOfJoperiodeCollectionNewJoperiode = joperiodeCollectionNewJoperiode.getPeriode();
                    joperiodeCollectionNewJoperiode.setPeriode(periode);
                    joperiodeCollectionNewJoperiode = em.merge(joperiodeCollectionNewJoperiode);
                    if (oldPeriodeOfJoperiodeCollectionNewJoperiode != null && !oldPeriodeOfJoperiodeCollectionNewJoperiode.equals(periode)) {
                        oldPeriodeOfJoperiodeCollectionNewJoperiode.getJoperiodeCollection().remove(joperiodeCollectionNewJoperiode);
                        oldPeriodeOfJoperiodeCollectionNewJoperiode = em.merge(oldPeriodeOfJoperiodeCollectionNewJoperiode);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                PeriodePK id = periode.getPeriodePK();
                if (findPeriode(id) == null) {
                    throw new NonexistentEntityException("The periode with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(PeriodePK id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Periode periode;
            try {
                periode = em.getReference(Periode.class, id);
                periode.getPeriodePK();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The periode with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<Ecriturec> ecriturecCollectionOrphanCheck = periode.getEcriturecCollection();
            for (Ecriturec ecriturecCollectionOrphanCheckEcriturec : ecriturecCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Periode (" + periode + ") cannot be destroyed since the Ecriturec " + ecriturecCollectionOrphanCheckEcriturec + " in its ecriturecCollection field has a non-nullable periode field.");
            }
            Collection<Budgetcgpb> budgetcgpbCollectionOrphanCheck = periode.getBudgetcgpbCollection();
            for (Budgetcgpb budgetcgpbCollectionOrphanCheckBudgetcgpb : budgetcgpbCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Periode (" + periode + ") cannot be destroyed since the Budgetcgpb " + budgetcgpbCollectionOrphanCheckBudgetcgpb + " in its budgetcgpbCollection field has a non-nullable periode field.");
            }
            Collection<Budgetpb> budgetpbCollectionOrphanCheck = periode.getBudgetpbCollection();
            for (Budgetpb budgetpbCollectionOrphanCheckBudgetpb : budgetpbCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Periode (" + periode + ") cannot be destroyed since the Budgetpb " + budgetpbCollectionOrphanCheckBudgetpb + " in its budgetpbCollection field has a non-nullable periode field.");
            }
            Collection<Joperiode> joperiodeCollectionOrphanCheck = periode.getJoperiodeCollection();
            for (Joperiode joperiodeCollectionOrphanCheckJoperiode : joperiodeCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Periode (" + periode + ") cannot be destroyed since the Joperiode " + joperiodeCollectionOrphanCheckJoperiode + " in its joperiodeCollection field has a non-nullable periode field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Exercicecompta exercicecompta = periode.getExercicecompta();
            if (exercicecompta != null) {
                exercicecompta.getPeriodeCollection().remove(periode);
                exercicecompta = em.merge(exercicecompta);
            }
            em.remove(periode);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Periode> findPeriodeEntities() {
        return findPeriodeEntities(true, -1, -1);
    }

    public List<Periode> findPeriodeEntities(int maxResults, int firstResult) {
        return findPeriodeEntities(false, maxResults, firstResult);
    }

    private List<Periode> findPeriodeEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Periode.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Periode findPeriode(PeriodePK id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Periode.class, id);
        } finally {
            em.close();
        }
    }

    public int getPeriodeCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Periode> rt = cq.from(Periode.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
